//
//  BottomBar.m
//  QSBK
//
//  Created by Eric Wang on 4/17/15.
//  Copyright (c) 2015 Eric Wang. All rights reserved.
//

#import "BottomBar.h"
#define buttonMakgin ScreenW/4-29

@implementation BottomBar
- (instancetype)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if(self)
    {
        [self addAllSubViews];
        
    }
    return self;
}

- (void)addAllSubViews
{
    CGFloat marginX = ScreenW/2-1.0;
    CGFloat marginY = self.frame.size.height/6;
    
    
    self.publishButton = [[BottomButton alloc]init];
    self.publishButton.bottomButtonType = ButtonTypePublish;
    [self.publishButton setImage:[UIImage imageNamed:@"submit.png"] forState:UIControlStateNormal];
    [self.publishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.publishButton setTitle:@"投稿" forState:UIControlStateNormal];
    [self.publishButton addTarget:self action:@selector(bottomButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.publishButton.translatesAutoresizingMaskIntoConstraints = NO;
    
    self.checkButton = [[BottomButton alloc]init];
    self.checkButton.bottomButtonType = ButtonTypeCheck;
    [self.checkButton setImage:[UIImage imageNamed:@"examine.png"] forState:UIControlStateNormal];
    [self.checkButton setTitle:@"审帖" forState:UIControlStateNormal];
    [self.checkButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.checkButton addTarget:self action:@selector(bottomButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.checkButton.translatesAutoresizingMaskIntoConstraints = NO;
    
    
    UIView *margin = [[UIView alloc]init];
    margin.frame = CGRectMake(marginX, marginY, 2, self.frame.size.height*2/3);
    margin.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.publishButton];
    [self addSubview:self.checkButton];
    [self addSubview:margin];
    
    _constraint = [[NSMutableArray alloc]init];
    
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-buttonMargin-[publish]-buttonMargin-[margin]" options:0 metrics:@{@"buttonMargin":@(buttonMakgin)} views:@{@"publish":self.publishButton,@"margin":margin}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[publish]-10-|" options:0 metrics:nil views:@{@"publish":self.publishButton}]];
    
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[margin]-buttonMargin-[check]-buttonMargin-|" options:0 metrics:@{@"buttonMargin":@(buttonMakgin)} views:@{@"margin":margin,@"check":self.checkButton}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[check]-10-|" options:0 metrics:nil views:@{@"check":self.checkButton}]];
    
    [self addConstraints:_constraint];
}

- (void)bottomButtonClick:(BottomButton *)button
{
    if(button.bottomButtonType == ButtonTypePublish)
    {
        if(self.delegate!=nil&&[self.delegate respondsToSelector:@selector(openPublishView)])
           {
               [self.delegate openPublishView];
           }
    }else if (button.bottomButtonType == ButtonTypeCheck)
    {
        if(self.delegate!=nil&&[self.delegate respondsToSelector:@selector(openCheckView)])
        {
            [self.delegate openCheckView];
        }
    }
}
@end
